package com.frxuexi.document

import com.frxuexi.lib_base.BaseRepository
import com.frxuexi.lib_base.entity.ApiResponse
import com.frxuexi.lib_http.HttpType
import com.frxuexi.lib_http.NetWorkFactory
import kotlinx.coroutines.flow.Flow
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.MultipartBody
import okhttp3.RequestBody
import retrofit2.http.Body
import retrofit2.http.Part
import java.io.File
import javax.inject.Inject


@OptIn
class DocRepository @Inject constructor():BaseRepository() {
    val apiService by lazy { NetWorkFactory.factory(HttpType.NONE).create(ApiService::class.java) }

    fun getDocs(map: Map<String,Int>): Flow<ApiResponse<List<DocEntity>>>{
        return apiService.getDocs(mapToBody(map))
    }

    fun upload(name:String,path:String):Flow<ApiResponse<List<DocEntity>>>{
        val body = RequestBody.create("multipart/form-data".toMediaTypeOrNull(), File(path))
        val multipartBody = MultipartBody.Builder().addFormDataPart("file", name, body).build()
        return apiService.upload(multipartBody.parts)
    }

    fun delete(map: Map<String, Int>):Flow<ApiResponse<Any>>{
        return apiService.delete(mapToBody(map))
    }
}